//
//  PolicyAdvisoryViewController.m
//  homeland
//
//  Created by SunShine on 13-10-29.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "PolicyAdvisoryViewController.h"
#import "PolicyAdvisoryCell.h"
#import "AdvisoryViewController.h"
#import "PolicyFileListViewController.h"

@interface PolicyAdvisoryViewController ()
@property (strong, nonatomic) PolicyAdvisoryListService *dataService;
@property (strong, nonatomic) NSString *phoneToCall;
@end

@implementation PolicyAdvisoryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"政策咨询";
    self.dataService = [PolicyAdvisoryListService new];
    _dataService.delegate = self;
    [self startLoadingView];
    [_dataService loadDataWithKey:nil];
    

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView setBackgroundView:nil];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataService.dataList count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PolicyAdvisoryCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"PolicyAdvisoryCell" owner:self options:nil] objectAtIndex:0];
    PolicyAdvisoryListItem *item = [_dataService.dataList objectAtIndex:indexPath.row];
    cell.nameLabe.text = item.unitName;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.phoneButton.tag = indexPath.row;
    [cell.phoneButton addTarget:self action:@selector(phoneButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.advisoryButton.tag = indexPath.row;
    [cell.advisoryButton addTarget:self action:@selector(advisoryButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.fileButton.tag = indexPath.row;
    [cell.fileButton addTarget:self action:@selector(fileButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    //隐藏政策文件图标,并调整按钮的位置
    cell.fileButton.hidden = YES;
    cell.phoneButton.frame = cell.advisoryButton.frame;
    cell.advisoryButton.frame = cell.fileButton.frame;
    [cell setBackgroundColor:[UIColor clearColor]];
    [cell.contentView setBackgroundColor:[UIColor clearColor]];
    return cell;
}

#pragma mark - Buttons
-(void)phoneButtonAction:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    NSObject *item = [self.dataService.dataList objectAtIndex:btn.tag];
    NSString* phone = [item valueForKey:@"phone"];
    if ([phone rangeOfString:@","].location != NSNotFound) {
        NSLog(@"准备截断电话号码%@",phone);
        phone = [phone substringToIndex:[phone rangeOfString:@","].location];
    }
    NSLog(@"点击了电话按钮%@",phone);
    self.phoneToCall = phone;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString  stringWithFormat:@"您是否需要拨打电话%@",phone] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.tag = PHONE_ALERT_TAG;
    [alert show];
}

-(void)advisoryButtonAction:(id)sender
{
    NSLog(@"点击了咨询按钮");
    AdvisoryViewController *advisoryVC = [[AdvisoryViewController alloc]initWithNibName:@"AdvisoryViewController" bundle:nil];
    advisoryVC.currentTitle=@"政策咨询";
    NSInteger index = ((UIButton *)sender).tag;
    PolicyAdvisoryListItem *item = [_dataService.dataList objectAtIndex:index];
    CommonMineQueryItem *commonItem = [CommonMineQueryItem new];
    commonItem.title = item.unitName;
    advisoryVC.config = [NSDictionary dictionaryWithObject:commonItem forKey:@"dataItem"];
    [self.navigationController pushViewController:advisoryVC animated:YES];
}

-(void)fileButtonAction:(id)sender
{
    NSInteger index = ((UIButton *)sender).tag;
    PolicyAdvisoryListItem *item = [_dataService.dataList objectAtIndex:index];
    PolicyFileListViewController *fileVC = [[PolicyFileListViewController alloc]initWithNibName:@"PolicyFileListViewController" bundle:nil];
    fileVC.unitName = item.unitName;
    [self.navigationController pushViewController:fileVC animated:YES];
}

#pragma mark - Alert View Delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == PHONE_ALERT_TAG) {
        if (buttonIndex == 1) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",_phoneToCall]]];
        }
    }
}

#pragma mark - Data Service
-(void)loadDataFinished
{
    [self stopLoadingView];
    [_tableView reloadData];
}

-(void)loadDataFaild:(NSString *)msg
{
    [self stopLoadingView];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
    [alert show];
}
@end
